<template>
  <div class="app-container">
    <div class="search-container">
      <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" class="mb8"
               label-width="80px">
        <el-form-item v-if="!userInfo.orgId">
          <select-org-tree :value="queryParams.orgId" @orgNodeClick="orgNodeClick"/>
        </el-form-item>
        <el-form-item label="流转时间">
          <el-date-picker v-model="dateRange" :unlink-panels="true" end-placeholder="结束日期"
                          range-separator="截止" start-placeholder="开始日期" type="datetimerange"
                          value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="库存单位" prop="storageId">-->
        <!--          <drug-storage-dept ref="storageIdDeptRef" :org-id="queryParams.orgId"-->
        <!--                             :value-storage-id="queryParams.storageId" class="import-maste width-120"-->
        <!--                             @drugStorageChange="storageIdClick"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="药品分类" prop="drugClass">
          <el-select v-model="queryParams.drugClass" class="width-120" clearable placeholder="请选择药品分类"
                     @change="handleQuery">
            <el-option v-for="dict in dictDataMap['drug_classification']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="药品名称" prop="drugName">
          <el-input v-model="queryParams.itemInfo" clearable placeholder="药品名称、编码、拼音码"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <!--                <el-form-item label="药品编码" prop="drugCode">-->
        <!--                    <el-input v-model="queryParams.drugCode" clearable placeholder="请输入药品编码"-->
        <!--                              @keyup.enter.native="handleQuery"/>-->
        <!--                </el-form-item>-->
        <el-form-item label="批号" prop="batchNo">
          <el-input v-model="queryParams.batchNo" clearable placeholder="请输入批号"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="">
          <el-checkbox v-model="queryParams.zeroStock" @change="handleQuery"><span
            class="el-form-item__label">零库存</span>
          </el-checkbox>
        </el-form-item>
        <el-form-item label="是否供应" prop="isSupply">
          <el-radio-group v-model="queryParams.isSupply" @change="handleQuery">
            <el-radio value="">全部</el-radio>
            <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                      :value="dict.dictValue">{{
                dict.dictLabel
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <div>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
          <el-button type="danger" @click="checkDownQuantityClick">查看低于库存下线药品</el-button>
        </div>
      </el-form>
    </div>
    <!--        <el-row :gutter="10" class="mb8">-->
    <!--            <el-col :span="1.5">-->
    <!--                <el-button-->
    <!--                    v-hasPerm="['base:stock:export']"-->
    <!--                    icon="el-icon-download"-->
    <!--                    plain-->
    <!--                    -->
    <!--                    type="warning"-->
    <!--                    @click="handleExport"-->
    <!--                >导出-->
    <!--                </el-button>-->
    <!--            </el-col>-->
    <!--        </el-row>-->
    <el-row class="mt10">
      <div class="ml10">
                <span class="el-form-item__label">总合计：进货价总额：<span class="fz19 font-color-red mr5">
                        {{ allPageTradePrice.toFixed(2) }}</span>元</span>
        <span class="el-form-item__label">零售价总额：<span class="fz19 font-color-red mr5">
                        {{ allPageRetailPrice.toFixed(2) }}</span>元</span>
        <span class="el-form-item__label mr80">进销差：<span class="fz19 font-color-red mr5">
                        {{ allBalance.toFixed(2) }}</span>元</span>
        <span class="el-form-item__label">
                    <span class="el-form-item__label">本页合计：进货价：
                        <span class="fz19 font-color-red mr5">{{ thisPageTradePrice.toFixed(2) }}</span>元
                    </span>
                    <span class="el-form-item__label">零售价总额:
                        <span class="fz19 font-color-red mr5">{{ thisPageRetailPrice.toFixed(2) }}</span>元
                    </span>
                    <span class="el-form-item__label">进销差：
                        <span class="fz19 font-color-red mr5">{{ thisBalance.toFixed(2) }}</span>元
                    </span>
                </span>
      </div>
    </el-row>
    <!--        库存列表-->
    <el-table v-adaptive :border="true" :data="stockList" :header-cell-style="$headerCellStyle" :stripe="true"
              highlight-current-row>
      <!--            <el-table-column :show-overflow-tooltip="true" :align="'center'" fixed="left" label="库存单位" prop="storageName"/>-->
      <el-table-column :align="'center'" :show-overflow-tooltip="true" fixed="left" label="药品编码"
                       prop="drugCode"
                       width="130px"/>
      <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" header-align="center" label="药品名称"
                       prop="drugName"
                       width="180px">
        <template #default="scope">
          <span class="fz14 fw-bold">{{ scope.row.drugName }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" fixed="left" label="药品分类"
                       prop="drugClassName"/>
      <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" label="生产商" prop="firmName"
                       width="150px"/>
      <!--            <el-table-column :show-overflow-tooltip="true" :align="'center'" fixed="left" label="供应商"-->
      <!--                             prop="supplierName" width="130px"/>-->
      <el-table-column :align="'right'" :show-overflow-tooltip="true" label="最小规格" prop="minSpec" width="100"/>
      <el-table-column :align="'left'" :show-overflow-tooltip="true" label="最小单位" prop="minUnits">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"/>
        </template>
      </el-table-column>
      <el-table-column :align="'left'" :show-overflow-tooltip="true" header-align="center" label="包装规格"
                       prop="packageSpec"
                       width="100px"/>
      <!-- <el-table-column :show-overflow-tooltip="true" :align="'left'" label="包装单位" prop="packageUnits">
          <template #default="scope">
              <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits" />
          </template>
      </el-table-column> -->
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="库存量" prop="quantity"
                       width="140px">
        <template #default="scope">
          <template v-if="scope.row.quantityMin > 0">
            <!--小包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              {{ scope.row.quantityMin }}
            </div>
          </template>
          <!--大包装-->
          <template v-if="scope.row.quantityMax > 0">
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">{{ scope.row.quantityMax }}
            </div>
          </template>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="进货单价(元)" prop="tradePrice"
                       width="100px">
        <template #default="scope">
          <span>{{ scope.row.packSize === '1' ? scope.row.tradePriceMin : scope.row.tradePrice }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="零售单价(元)" prop="retailPrice"
                       width="100px">
        <template #default="scope">
          <span>{{ scope.row.packSize === '1' ? scope.row.retailPriceMin : scope.row.retailPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="进货总价(元)" prop="tradePriceMin"
                       width="100px">
        <template #default="scope">
          <span>{{ scope.row.itemTradePriceShow }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="零售总价(元)" prop="retailPriceMin"
                       width="100px">
        <template #default="scope">
          <span>{{ scope.row.itemRetailPriceShow }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="进销差(元)" prop="" width="100px">
        <template #default="scope">
                    <span>{{
                        (scope.row.quantity * scope.row.retailPriceMin -
                          scope.row.quantity * scope.row.retailPriceMin).toFixed(2)
                      }}</span>
        </template>
      </el-table-column>
      <!--            <el-table-column :show-overflow-tooltip="true" :align="'center'" label="大包装进货单价(元)"-->
      <!--                             prop="retailPrice"/>-->
      <!--            <el-table-column :show-overflow-tooltip="true" :align="'center'" label="大包装零售单价(元)"-->
      <!--                             prop="tradePrice"/>-->
      <!--            <el-table-column :show-overflow-tooltip="true" :align="'center'" label="大包装库存量" prop="tradePrice"/>-->
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="批号" prop="batchNo"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="生产日期" prop="produceDate"
                       width="100">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="有效期" prop="expireDate"
                       width="100">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="失效天数" prop="daysOfFailure"
                       width="80px">
        <template #default="scope">
                    <span :style="scope.row.daysOfFailure <=0 ? 'color:red' : ''">
                        {{ scope.row.daysOfFailure <= 0 ? '已过期' : scope.row.daysOfFailure }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="入库单号" prop="documentNo"
                       width="160px"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="货位" prop="location"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="是否供应" prop="isSupply">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.isSupply"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="停供时间" prop="stopDate"
                       width="100">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.stopDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="药品剂型" prop="drugFromName"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="基药类型" prop="basicDrugType">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['basic_drug_type']" :value="scope.row.basicDrugType"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="抗菌药物" prop="antisepticType">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.antisepticType"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="限制等级" prop="limitClassName"/>

      <el-table-column :align="'left'" :show-overflow-tooltip="true" label="国家医保名称" prop="countryYbName"
                       width="180px"/>
      <el-table-column :align="'left'" :show-overflow-tooltip="true" label="国家医保编码" prop="countryYbCode"
                       width="200px"/>
    </el-table>
    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList"/>


    <el-dialog v-model="downQuantityOpen" :close-on-click-modal="false" :title="downQuantityTitle" append-to-body
               class="downQuantity-dialog" width="70%" @close="downQuantityCancel">
      <el-table :border="true" :data="downQuantityList" :header-cell-style="$headerCellStyle" :stripe="true"
                height="550"
                highlight-current-row>
        <el-table-column :align="'center'" fixed="left" header-align="center" label="序号" prop="rowIndex"
                         width="50">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" header-align="center"
                         label="药品名称" min-width="140"
                         prop="drugName">
          <template #default="scope">
            <span class="fz14 fw-bold">{{ scope.row.drugName }}</span>
          </template>
        </el-table-column>
        <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" label="生产商" prop="firmName"
                         width="120px"/>
        <el-table-column :align="'right'" :show-overflow-tooltip="true" label="包装规格" prop="packageSpec"
                         width="180px">
          <template #default="scope">
            <span class="ys-css">{{ scope.row.packageSpec }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column :align="'right'" :show-overflow-tooltip="true" label="包装数量" prop="packageNum" width="70"/>-->
        <el-table-column :align="'center'" label="下限" prop="actualQuantity" width="160">
          <template #default="scope">
            <template v-if="scope.row.minUnits != scope.row.packageUnits">
              <!--小包装-->
              <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                             style="float: right"/>
              <div style="float: right;margin-right: 5px;font-weight: bold;">
                {{ scope.row.lowerQuantityMinActual }}
              </div>
              <!--大包装-->
              <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                             style="float: right"/>
              <div style="float: right;margin-right: 5px;font-weight: bold;">
                {{ scope.row.lowerQuantityMaxActual }}
              </div>
            </template>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="库存量" prop="quantity"
                         width="160px">
          <template #default="scope">
            <template v-if="scope.row.quantity > 0">
              <template v-if="scope.row.quantityMin > 0">
                <!--小包装-->
                <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                               style="float: right"/>
                <div style="float: right;margin-right: 5px;font-weight: bold;">
                  <span style="color: #ea0d0d">{{ scope.row.quantityMin }}</span>
                </div>
              </template>
              <!--大包装-->
              <template v-if="scope.row.quantityMax > 0">
                <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                               style="float: right"/>
                <div style="float: right;margin-right: 5px;font-weight: bold;">
                  <span style="color: #ea0d0d">{{ scope.row.quantityMax }}</span>
                </div>
              </template>
            </template>
            <span v-else><dict-data-tag :options="dictDataMap['spec_unit']"
                                        :value="scope.row.packageUnits" style="float: right"/>
              <div
                style="float: right;margin-right: 5px;font-weight: bold;">0
              </div>
            </span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="半年内使用量" prop="useQuantity"
                         width="110px">
          <template #default="scope">
            <template v-if="scope.row.useQuantityMin > 0">
              <!--小包装-->
              <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                             style="float: right"/>
              <div style="float: right;margin-right: 5px;font-weight: bold;">
                <span style="color: #0334ea">{{ scope.row.useQuantityMin }}</span>
              </div>
            </template>
            <!--大包装-->
            <template v-if="scope.row.useQuantityMax > 0">
              <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                             style="float: right"/>
              <div style="float: right;margin-right: 5px;font-weight: bold;">
                <span style="color: #0334ea">{{ scope.row.useQuantityMax }}</span>
              </div>
            </template>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否国采" width="65">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.isNationalProcurement"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="计划采购数量" width="110">
          <template #default="scope">
            <el-input v-model="scope.row.purchasingQuantity" clearable>
              <template #append>
                <dict-data-tag :options="dictDataMap['spec_unit']"
                               :value="scope.row.packageUnits"/>
              </template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="售价(元)" width="100">
          <template #default="scope">
            <el-input v-model="scope.row.retailPrice"/>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div style="flex: auto">
          <el-button plain type="danger" @click="emptyDrugAmount()">
            <template #icon>
              <i-ep-delete-filled/>
            </template>
            清空
          </el-button>
          <el-button type="primary" @click="confirmClick">
            生成采购计划
          </el-button>
          <el-button @click="downQuantityCancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {selectPageStock} from "@/api/drug/drugStock";
import {getCheckDownQuantity} from "@/api/drug/drugStorageProfile.ts";
import {addDrugPurchasingPlan} from "@/api/drug/drugPurchasingPlan.ts"
import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";

const useUserStore = useUserStoreHook();
const {proxy} = getCurrentInstance() as any
const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('spec_unit', 'sys_yes_no', 'basic_drug_type', 'drug_classification'))
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo
const currentSelectDept = proxy.$currentSelectDept
const storageIdDeptRef = ref()
const dateRange = ref([])

// 低于药品库存下线的药品列表
const downQuantityList = ref<any>([])

const downQuantityOpen = ref(false)

const downQuantityTitle = ref()
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 药品库存表格数据
const stockList = ref([])
// 全部页进货价
const allPageTradePrice = ref(0)
// 全部页零售价价
const allPageRetailPrice = ref(0)
// 全部页进销差
const allBalance = ref(0)
// 本页进货价
const thisPageTradePrice = ref(0)
// 本页零售价价
const thisPageRetailPrice = ref(0)
// 本页进销差
const thisBalance = ref(0)
// 查询参数
const queryParamsRef = ref()
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  storageId: currentSelectDept,
  drugClass: undefined,
  zeroStock: false,
  drugCode: undefined,
  drugInfoId: undefined,
  priceId: undefined,
  packSize: undefined,
  storageName: undefined,
  packageNum: undefined,
  packageSpec: undefined,
  packageUnits: undefined,
  minSpec: undefined,
  minUnits: undefined,
  tradePrice: undefined,
  tradePriceMin: undefined,
  retailPrice: undefined,
  retailPriceMin: undefined,
  networkPrice: undefined,
  networkPriceMin: undefined,
  discount: undefined,
  batchNo: undefined,
  produceDate: undefined,
  expireDate: undefined,
  quantity: undefined,
  orgId: undefined,
  documentNo: undefined,
  importMasterId: undefined,
  location: undefined,
  supplierId: undefined,
  inventoryIsShow: undefined,
  remarks: undefined,
  isSupply: '',
  stopOperator: undefined,
  stopDate: undefined,
  proportion: undefined
})

if (userInfo.orgId) {
  queryParams.value.orgId = userInfo.orgId
  getList()
}

// 机构树点击事件
function orgNodeClick(data, _e) {
  queryParams.value.orgId = data.orgId
  if (queryParams.value.orgId) {
    getList()
  }
}

/** 查询药品库存列表 */
function getList() {

  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty)
  }
  selectPageStock(proxy.$addDateRange(queryParams.value, dateRange.value)).then((response: any) => {
    stockList.value = response.data.tableDataInfo.records;
    total.value = response.data.tableDataInfo.total;
    thisPageTradePrice.value = 0
    thisPageRetailPrice.value = 0
    thisBalance.value = 0
    allPageRetailPrice.value = response.data.allPageRetailPrice
    allPageTradePrice.value = response.data.allPageTradePrice
    allBalance.value = response.data.allBalance
    stockList.value.forEach((item: any) => {
      let itemTradePriceShow = Number(item.packSize === '1' ? item.quantity * item.tradePriceMin : item.quantity / item.packageNum * item.tradePrice)
      let itemRetailPriceShow = Number(item.packSize === '1' ? item.quantity * item.retailPriceMin : item.quantity / item.packageNum * item.retailPrice)
      item.itemTradePriceShow = itemTradePriceShow.toFixed(2)
      item.itemRetailPriceShow = itemRetailPriceShow.toFixed(2)
      thisPageTradePrice.value += itemTradePriceShow
      thisPageRetailPrice.value += itemRetailPriceShow
      thisBalance.value += thisPageRetailPrice.value - thisPageTradePrice.value
      item.quantityMax = Math.floor(item.quantity / item.packageNum)
      item.quantityMin = item.quantity % item.packageNum

    })
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  dateRange.value = []
  storageIdDeptRef.value.setValue("")
  handleQuery();
}

function storageIdClick(obj) {
  queryParams.value.storageId = obj.id
  getList();
}

/**
 * 查看低于库存下线药品
 */
function checkDownQuantityClick() {
  downQuantityOpen.value = true
  const params = {
    storageId: queryParams.value.storageId,
    orgId: queryParams.value.orgId,
    confirmType: "0"
  }
  getCheckDownQuantity(params).then((res: any) => {
    downQuantityList.value = res.data
    downQuantityList.value.forEach((item: any) => {
      // 上限小包装数量
      const upperQuantity = item.upperQuantity
      // 现有库存小包装数量
      const quantity = item.quantity
      // 现有库存量 大包装数量
      item.quantityMax = Math.floor(quantity / item.packageNum)
      // 现有库存量 除完以后剩余小包装数量
      item.quantityMin = quantity % item.packageNum
      // 半年内使用量 大包装数量
      item.useQuantityMax = Math.floor(item.useQuantity / item.packageNum)
      item.useQuantityMin = item.useQuantity % item.packageNum
      // 上限 - 现有库存量 = 计划采购数量
      const balance = upperQuantity - quantity
      item.purchasingQuantity = (balance / item.packageNum).toFixed(0)
      item.lowerQuantityMin = item.lowerQuantity % item.packageNum
      // 设置最上线和最小线数据
      if (item.lowerQuantity > 0) {
        // 数量/包装数量 的除数
        const bigNum = Math.floor(item.lowerQuantity / item.packageNum)
        // 数量/包装数量 的余数
        const minNum = item.lowerQuantity % item.packageNum
        item.lowerQuantityMaxActual = bigNum
        item.lowerQuantityMinActual = minNum
        if (item.packageUnits === item.minUnits && minNum <= 0) {
          item.lowerQuantityMinActual = bigNum
        }
      }
    })
  })
}

function downQuantityCancel() {
  downQuantityOpen.value = false
  downQuantityList.value = []
}

/**
 * 生成采购计划
 */
function confirmClick() {
  ElMessageBox.confirm(
    "确定要生成采购计划吗？",
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      let indexNum: number = 0;
      const downQuantityListThis = downQuantityList.value.filter((item: any) => {
        return item.purchasingQuantity > 0
      })
      downQuantityListThis.forEach((item: any, _index: number) => {
        indexNum = Number(indexNum) + 1;
        item.serialNumber = indexNum
        item.packageUnitsName = item.packageUnitName
        item.purchasingQuantity = item.packageNum * item.purchasingQuantity
        item.useQuantity = item.packageNum * item.useQuantity
        item.lowerQuantity = item.packageNum * item.lowerQuantity
        item.confirmType = '0'
      })
      addDrugPurchasingPlan({
        // 过滤掉计划采购数量为0的数据
        downQuantityList: downQuantityListThis,
        orgId: queryParams.value.orgId,
        confirmType: '2',
        purchasingPlanNo: downQuantityList.value[0].purchasingPlanNo,
        // 是否新增
        isNowInsert: true,
      }).then(() => {
        ElMessage.success("保存成功")
        downQuantityCancel()
      })
    })
}

/**
 * 清空计划采购数量
 */
function emptyDrugAmount() {
  if (downQuantityList != null && downQuantityList.value.length > 0) {
    downQuantityList.value.forEach((item: any) => {
      item.purchasingQuantity = ''
    })
  }
}
</script>
<script lang="ts">
export default {
  name: "DrugStockIndex"
}
</script>

<style scoped>
:deep(.vue-treeselect__control) {
  height: 30px !important;
  margin-bottom: 13px;
}

:deep(.vue-treeselect__single-value) {
  top: -2px
}

:deep(.el-input-group__append) {
  padding: 0 5px;
  width: 40px;
}
</style>

<style>
.downQuantity-dialog .el-dialog__body {
  margin-right: 10px !important;
}
</style>
